package com.jams.goodox.utils;

import android.content.Context;
import android.os.Handler;
import android.support.annotation.StringRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;

/**
 * 提示工具类
 */
public class ToastUtil {

    private static final String NET_ERROR_HINT = "网络请求失败，请重新加载";//后期考虑不需要

    private static Toast toast = null;
    private static Toast mToast;

    /**
     * 显示Toast提示，传入提示字符串
     *
     * @param info 需要显示的参数
     */
    public static void show(String info) {
        if (null == mToast) {
            mToast = Toast.makeText(TownApplicationLike.getInstance(), info, Toast.LENGTH_SHORT);
        } else {
            mToast.setText(info);
        }
        mToast.show();
    }

    /**
     * 自定义短时间显示提示
     */
    public static void showShort(Context context, String message) {
        show(context, message);
    }

    /**
     * @param context 传入context
     * @param message 显示的字符串
     */
    public static void show(Context context, String message) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.custom_toast, null);//自定义布局
            TextView text = (TextView) view.findViewById(R.id.toast_message);//显示的提示文字
            text.setText(message);
            mHandler.removeCallbacks(r);
            if (toast == null) {//只有mToast==null时才重新创建，否则只需更改提示文字
                toast = new Toast(context);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM, 0, 150);
            }
            toast.setView(view);
            mHandler.postDelayed(r, 2000);//延迟1秒隐藏toast
            toast.show();
    }

    public static void show(Context context, @StringRes int info) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View view = inflater.inflate(R.layout.custom_toast, null);//自定义布局
            TextView text = (TextView) view.findViewById(R.id.toast_message);//显示的提示文字
            text.setText(context.getString(info));
            mHandler.removeCallbacks(r);
            if (toast == null) {//只有mToast==null时才重新创建，否则只需更改提示文字
                toast = new Toast(context);
                toast.setDuration(Toast.LENGTH_SHORT);
                toast.setGravity(Gravity.BOTTOM, 0, 150);
            }
            toast.setView(view);
            mHandler.postDelayed(r, 2000);//延迟1秒隐藏toast
            toast.show();
    }

    private static Handler mHandler = new Handler();
    private static Runnable r = new Runnable() {
        public void run() {
            toast.cancel();
            toast = null;//toast隐藏后，将其置为null
        }
    };

    //提示网络请求错误
    public static void showNetErrorToast() {
        showShort(TownApplicationLike.getInstance(), NET_ERROR_HINT);
    }

    //长时间显示
    public static void showLong(Context context, String message) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_toast, null);//自定义布局
        TextView text = (TextView) view.findViewById(R.id.toast_message);//显示的提示文字
        text.setText(message);
        mHandler.removeCallbacks(r);
        if (toast == null) {//只有mToast==null时才重新创建，否则只需更改提示文字
            toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.BOTTOM, 0, 150);
        }
        toast.setView(view);
        mHandler.postDelayed(r, 3000);//延迟1秒隐藏toast
        toast.show();
    }
    public static void showMaxLengthTip(Context context,int size){
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.custom_toast, null);//自定义布局
        TextView text = (TextView) view.findViewById(R.id.toast_message);//显示的提示文字
        text.setText("不能超过"+size+"字");
        mHandler.removeCallbacks(r);
        if (toast == null) {//只有mToast==null时才重新创建，否则只需更改提示文字
            toast = new Toast(context);
            toast.setDuration(Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.BOTTOM, 0, 150);
        }
        toast.setView(view);
        mHandler.postDelayed(r, 3000);//延迟1秒隐藏toast
        toast.show();
    }

    public static void dismiss(){
        if(toast != null){
            toast.cancel();
        }
        if(mToast != null){
            mToast.cancel();
        }
    }
}
